﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ProductService.Entity;

namespace ProductService
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            //Configuration = configuration;
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile("ConsulConfig.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();


            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            #region Swagger
            services.AddSwaggerGen(options => {

                options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Version = "v1",
                    Title = " API 文档",
                    Description = "by lv"
                });
            });
            #endregion

            services.AddOptions();
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }


            #region config

            var ip = Configuration["ConsulConfig:ip"];
            int.TryParse(Configuration["ConsulConfig:port"], out int prot);
            var checkUrl = Configuration["ConsulConfig:check"];
            var url = Configuration["ConsulConfig:agentUrl"];
            #endregion

            #region consul
            var client = new ConsulClient((obj) =>
            {
                obj.Address = new Uri(url);
                obj.Datacenter = "dc1";
            });
            //注册到consul
            client.Agent.ServiceRegister(new AgentServiceRegistration()
            {
                ID = Assembly.GetExecutingAssembly().GetName().Name + Guid.NewGuid().ToString(),
                Name = Assembly.GetExecutingAssembly().GetName().Name,
                Address = ip,
                Port = prot,
                Check = new AgentServiceCheck()
                {
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),
                    Interval = TimeSpan.FromSeconds(10),
                    HTTP = $"http://10.10.11.224:8888/api/base/CheckHealth",
                    Timeout = TimeSpan.FromSeconds(5)
                }
            }).Wait();
            #endregion

            #region Swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.DocumentTitle = "swagger接口";
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
                c.RoutePrefix = "swagger";
            });
            #endregion
            app.UseMvc();
        }

        private static void ConfigurationOverview(ConsulClientConfiguration obj)
        {
            obj.Address = new Uri("http://10.10.20.128:8500");
            obj.Datacenter = "dc1";
        }
    }
}
